package com.ys.controller.api.business;

import com.ys.entity.base.TbCountry;
import com.ys.entity.business.TcwVatPaymentAccount;
import com.ys.service.business.TcwVatPaymentAccountService;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
@RequestMapping("/api/inter/tcwVatPaymentAccount")
public class ApiInterTcwVatPaymentAccountController {
    private static final Logger logger = LoggerFactory.getLogger(ApiInterTcwVatPaymentAccountController.class);

    @Autowired
    private CommonApiService<TcwVatPaymentAccount> commonApiService;

    @Autowired
    private CommonApiService<TbCountry> TbCountryCommonApiService;


    @Autowired
    private TcwVatPaymentAccountService tcwVatPaymentAccountService;

    @RequiresPermissions("tcwVatPaymentAccount:view")
    @RequestMapping("/search")
    @ResponseBody
    public LayuiTableVoJA search(@ModelAttribute TcwVatPaymentAccount cond){

        cond.setOrderby("A.createTime DESC")
            .addField("B.Code AS countryCode")
            .addField("B.Name AS countryName")
            .addField("C.Symbol AS amazonShopExchangeRate")
            .addField("(D.UserId + '/' + D.UserName) AS _ys_nameLinked")
            .addLeftJoin("TB_Country B on B.Code=A.CountryCode")
            .addLeftJoin("TP_AmazonShopExchangeRate C on C.Id=A.AmazonShopExchangeRateld")
            .addLeftJoin("TS_UserChange D on D.Id=A.CreateChangeld");
        return commonApiService.searchLay(cond);
    }



    /**
     * 新增
     */
    @RequestMapping("/toAdd")
    @ResponseBody
    public R toAdd(HttpServletRequest request, @RequestParam Map<String, Object> params
    		) {
        try {
            logger.info("新增传参：" + params);
            //修改
            return tcwVatPaymentAccountService.add(params);
        } catch (Exception e) {
            logger.error("新增出错：", e.getMessage(), e);
            return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/toEdit")
    @ResponseBody
    public R toEdit(HttpServletRequest request, @RequestParam Map<String, Object> params
    ) {
        try {
            logger.info("修改传参：" + params);
            //修改
            return tcwVatPaymentAccountService.edit(params);
        } catch (Exception e) {
            logger.error("修改出错：", e.getMessage(), e);
            return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
        }
    }
    
    @RequestMapping("/searchForModal")
    @ResponseBody
    public LayuiTableVoJA searchForModal(@ModelAttribute TcwVatPaymentAccount cond){
        return commonApiService.searchLay(cond);
    }

    /**
     * 弹出框-
     */
    @RequestMapping("/searchForCountry")
    @ResponseBody
    public LayuiTableVoJA searchForCountry(@ModelAttribute TbCountry cond){
        cond.setStatus(1);
        cond.addLeftJoin("TCW_VatPaymentAccount O ON O.CountryCode = A.Code ")
                .addField("O.Name AS countryName")
                .addField("O.Id AS amazonShopExchangeRateld");

        //已新增的不能再信息
        cond.where("( SELECT COUNT ( 1 ) AS num FROM TCW_VatPaymentAccount O ON O.CountryCode = A.Code ) = 0 ");

        return TbCountryCommonApiService.searchLay(cond);
    }

}
